package com.wosai.register.repository.jdbc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

import com.wosai.register.model.Terminal;
import com.wosai.register.model.UserInfo;
import com.wosai.register.repository.TerminalRepository;
import com.wosai.register.repository.UserRepository;

@Repository
public class JdbcUserRepository implements UserRepository {
    private final static Log logger = LogFactory.getLog(JdbcUserRepository.class);

    private NamedParameterJdbcTemplate npjt;

    private final static String SELECT_BY_USERNAME = "select * from USER_INFO where user_id = :username";
    private final static String SELECT_BY_MOBILE = "select * from USER_INFO where mobile = :mobile";
    
    private final static String UPDATE_PASSWORD = "update USER_INFO set user_pwd = :password where user_id = :username or mobile = :username";
    
    private final static String SELECT_TERMINALS = "select t.merchant_id, t.branch_id, t.terminal_id, t.branch_name from TERMINAL t, USER_MER_MAP um, USER_INFO ui " +
    		                                       "where (ui.user_id = :username or ui.mobile = :username) and ui.user_id = um.user_id and um.mer_id = t.merchant_id and um.branch_id = t.branch_id ";
    
    private static final ColumnMapRowMapper columnMapRowMapper = new ColumnMapRowMapper();

    @Autowired
    public JdbcUserRepository(DataSource dataSource) {
        this.npjt = new NamedParameterJdbcTemplate(dataSource);
    }

    @Override
    public UserInfo findByUsername(String username) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("username", username);
        return this.npjt.queryForObject(SELECT_BY_USERNAME, params, ParameterizedBeanPropertyRowMapper.newInstance(UserInfo.class));
    }

    @Override
    public UserInfo findByMobile(String mobile) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("mobile", mobile);
        return this.npjt.queryForObject(SELECT_BY_MOBILE, params, ParameterizedBeanPropertyRowMapper.newInstance(UserInfo.class));
    }

    @Override
    public void updatePassword(String username, String password) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("username", username);
        params.put("password", password);
        int rows = this.npjt.update(UPDATE_PASSWORD, params);
        if (rows < 1){
            throw new IllegalArgumentException(username + " doesn't exisit.");
        }
    }

    @Override
    public List<Map<String, Object>> findTerminalsByUser(String username) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("username", username);
        return this.npjt.query(SELECT_TERMINALS, params, columnMapRowMapper);
    }

}
